// components/self-swiper.js 自动播放首个视频，切换swiper视频播放暂停
//todo  模拟器视频自动播放，暂停不生效
Component({
  lifetimes: {
    attached: function () {
      this.initSwiper()
    },
    detached: function () {

    }
  },
  /**
   * 组件的属性列表
   */
  properties: {
    jsonObj: {
      type: Object,
      value: {}
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    videoIndex: 0
  },

  /**
   * 组件的方法列表
   */
  methods: {
    initSwiper() {
      //获取数据 swiperUrls数组:包含image,video的url
      let mediaArr = this.properties.jsonObj.value
      // console.log(999,mediaArr);
      for (let i = 0; i <= mediaArr.length; i++) {
        let data = {}
        if (mediaArr[i] && mediaArr[i].type == "video") {
          data['videoCtx' + i] = wx.createVideoContext('myVideo' + i, this)
          this.setData(data)
          // console.log(888,this.data);
        }
      }
      // console.log(777,this.data.videoIndex == 0,this.data['videoCtx0']);
      //初始化自动播放video
      if (this.data.videoIndex == 0 && this.data['videoCtx0']) {
        this.data['videoCtx0'].play()
        // console.log(666);
      }
    },
    swipChange(e) {
      let _self = this
      // console.log("e.detail.current", e.detail.current)
      if (_self.data.videoIndex != e.detail.current) {
        let str = 'videoCtx' + _self.data.videoIndex
        let strNow = 'videoCtx' + e.detail.current
        if (_self.data[str]) {
          _self.data[str].pause()
        }
        if (_self.data[strNow]) {
          _self.data[strNow].play()
        }
        _self.setData({
          videoIndex: e.detail.current
        })
      }
    },
  }
})